// internal/domain/event.go
package domain

import (
	"context"
	"time"
)

// EventService 事件服务接口
type EventService interface {
	// 订阅事件
	Subscribe(ctx context.Context, filter *EventFilter) (<-chan *Event, error)

	// 取消订阅
	Unsubscribe(subscriptionID string)

	// 获取历史事件
	GetEvents(ctx context.Context, filter *EventFilter, limit int) ([]*Event, error)

	// 启动事件监听
	StartListening(ctx context.Context, connID string) error

	// 停止事件监听
	StopListening(ctx context.Context, connID string) error
}

// Event 事件
type Event struct {
	ID        string         `json:"id"`
	Type      EventType      `json:"type"`
	Category  EventCategory  `json:"category"`
	Source    string         `json:"source"` // 连接 ID
	Target    string         `json:"target"` // VM/网络/存储池名称
	Action    string         `json:"action"` // started, stopped, created, deleted, etc.
	Detail    string         `json:"detail"`
	Metadata  map[string]any `json:"metadata,omitempty"`
	Timestamp time.Time      `json:"timestamp"`
}

type EventType string

const (
	EventTypeVM      EventType = "vm"
	EventTypeNetwork EventType = "network"
	EventTypeStorage EventType = "storage"
	EventTypeHost    EventType = "host"
	EventTypeError   EventType = "error"
)

type EventCategory string

const (
	EventCategoryLifecycle EventCategory = "lifecycle"
	EventCategoryConfig    EventCategory = "config"
	EventCategoryHealth    EventCategory = "health"
	EventCategoryError     EventCategory = "error"
)

// EventFilter 事件过滤器
type EventFilter struct {
	Types      []EventType     `json:"types,omitempty"`
	Categories []EventCategory `json:"categories,omitempty"`
	Sources    []string        `json:"sources,omitempty"` // 连接 ID 列表
	Targets    []string        `json:"targets,omitempty"` // VM 名称列表
	Actions    []string        `json:"actions,omitempty"`
	StartTime  *time.Time      `json:"start_time,omitempty"`
	EndTime    *time.Time      `json:"end_time,omitempty"`
}
